#!/srv/newsblur/venv/newsblur3/bin/python

import os

from vendor.munin.cassandra import MuninCassandraPlugin


class CassandraCFCountsPlugin(MuninCassandraPlugin):
    title = "read/write rate"
    args = "--base 1000 -l 0"
    vlabel = "ops per ${graph_period}"

    @property
    def fields(self):
        fs = []
        cfstats = self.cfstats()
        for kf, kfstats in cfstats.items():
            if not self.keyspaces or kf not in self.keyspaces:
                continue
            for cf, cfstats in kfstats['cf'].items():
                name = "%s_%s_reads" % (kf, cf)
                label = "%s.%s reads" % (kf, cf)
                fs.append((name, dict(
                        label = label,
                        info = label,
                        type = "DERIVE",
                        min = "0",
                    )))
                name = "%s_%s_writes" % (kf, cf)
                label = "%s.%s writes" % (kf, cf)
                fs.append((name, dict(
                        label = label,
                        info = label,
                        type = "DERIVE",
                        min = "0",
                    )))
        return fs

    def execute(self):
        cfstats = self.cfstats()
        values = {}
        for kf, kfstats in cfstats.items():
            if not self.keyspaces or kf not in self.keyspaces:
                continue
            for cf, cfstats in kfstats['cf'].items():
                name = "%s_%s" % (kf, cf)
                values["%s_reads" % name] = cfstats['Read Count']
                values["%s_writes" % name] = cfstats['Write Count']
        return values

if __name__ == "__main__":
    CassandraCFCountsPlugin().run()
